package com.abner.springcloud.consumer.movie.controller;

import com.abner.springcloud.consumer.movie.engity.User;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable String id){
        log.info("[{}] -- start query user information , id : [{}]",this.getClass(),id);
        User user = null;
        try {
            user = restTemplate.getForObject("http://SPRINGCLOUD-PROVIDER-USER/user/get/"+id,User.class);
        }catch (Exception e){
            log.error("[{}],query user is error , exception is : [{}]",this.getClass(),e);
        }
        return user;
    }

    @GetMapping("/log-user-instance")
    public void logUserInstance(){
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("SPRINGCLOUD-PROVIDER-USER");
        log.info("{}:{}:{}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
    }
}